/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bestco.bela.demo;

import com.bestco.bela.EntityClasses.Login;
import com.bestco.bela.demo.exceptions.NonexistentEntityException;
import com.bestco.bela.demo.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import com.bestco.bela.EntityClasses.Creditlevel;

/**
 *
 * @author bash
 */
public class LoginJpaController {

    public LoginJpaController() {
        emf = Persistence.createEntityManagerFactory("LoginFramworkPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Login login) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Creditlevel idCredit = login.getIdCredit();
            if (idCredit != null) {
                idCredit = em.getReference(idCredit.getClass(), idCredit.getIdCredit());
                login.setIdCredit(idCredit);
            }
            em.persist(login);
            if (idCredit != null) {
                idCredit.getLoginCollection().add(login);
                idCredit = em.merge(idCredit);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findLogin(login.getUsername()) != null) {
                throw new PreexistingEntityException("Login " + login + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Login login) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Login persistentLogin = em.find(Login.class, login.getUsername());
            Creditlevel idCreditOld = persistentLogin.getIdCredit();
            Creditlevel idCreditNew = login.getIdCredit();
            if (idCreditNew != null) {
                idCreditNew = em.getReference(idCreditNew.getClass(), idCreditNew.getIdCredit());
                login.setIdCredit(idCreditNew);
            }
            login = em.merge(login);
            if (idCreditOld != null && !idCreditOld.equals(idCreditNew)) {
                idCreditOld.getLoginCollection().remove(login);
                idCreditOld = em.merge(idCreditOld);
            }
            if (idCreditNew != null && !idCreditNew.equals(idCreditOld)) {
                idCreditNew.getLoginCollection().add(login);
                idCreditNew = em.merge(idCreditNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = login.getUsername();
                if (findLogin(id) == null) {
                    throw new NonexistentEntityException("The login with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Login login;
            try {
                login = em.getReference(Login.class, id);
                login.getUsername();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The login with id " + id + " no longer exists.", enfe);
            }
            Creditlevel idCredit = login.getIdCredit();
            if (idCredit != null) {
                idCredit.getLoginCollection().remove(login);
                idCredit = em.merge(idCredit);
            }
            em.remove(login);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Login> findLoginEntities() {
        return findLoginEntities(true, -1, -1);
    }

    public List<Login> findLoginEntities(int maxResults, int firstResult) {
        return findLoginEntities(false, maxResults, firstResult);
    }

    private List<Login> findLoginEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Login as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Login findLogin(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Login.class, id);
        } finally {
            em.close();
        }
    }

    public int getLoginCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Login as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
